<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\api\model;

use think\Model as ThinkModel;
use think\Db;

/**
 * 字符串处理模型
 * @package app\api\model
 */
class Str extends ThinkModel
{
	// 设置当前模型对应的完整数据表名称
	protected $name = 'api_send';
	
	// 自动写入时间戳
	protected $autoWriteTimestamp = true;
	
	/**
	 * 格式化请求字符串
	 * @param string str 字符串
	 * @return array 格式化后数组
	 */
	public static function format_str($str){
		$arr = [];
		$length = self::mb_substr2($str, 0, 4); //截取4位报文长度
		$body = self::mb_substr2($str, 4, (int)$length); //截取$length位报体长度
		$type = trim(self::mb_substr2($body, 0, 20));  //截取20位请求类型
		$type_name = self::type($type);
		$arr = [
			'length'	=> $length,
			'body'		=> $body,
			'type'		=> $type,
		];
		if($type_name){
			$arr['body'] = mb_convert_encoding($arr['body'], 'UTF-8', 'GBK'); //转utf-8
			$query = self::create($arr);
			$arr['id'] = $query->id;
		}
		return $arr;
	}
	
	/**
	 * 用空格补全长度
	 * @param string str 字符串
	 * @param int end 总长度
	 * @return string 补全后的字符串
	 */
	public static function addSpace($str, $end, $type = 1){
		$arr = self::ch2arr($str);
		$len = 0;
		foreach($arr as $v){
			if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$v)){
				$len = $len + 2;	#每个中文字符长度等于2
			}else{
				$len++;
			}
		}
		$add = $end - $len;
		$space = '';
		if($add > 0){
			for($i = 0;$i < $add;$i++){
				$space .= ' ';
			}
		}
		// 默认右补空格
		if($type === 1){
			$str = $str.$space;
		}else{
			$str = $space.$str;
		}
		return $str;
	}
	
	#截取字符串
	public static function mb_substr2($str,$start,$end)
	{
		$arr = self::ch2arr($str);		#将字符串装换成数组
		$len = 0;
		$result = '';
		foreach($arr as $v){		
			if($start <= $len && $len < $start + $end){
				$result .= $v;
			}
			if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$v)){
				$len = $len + 2;	#每个中文字符长度等于2
			}else{
				$len++;
			}
		}
		
		return $result;
	}
	
	/**
	 * 获取字符串长度
	 * @param  string $str 字符串
	 * @return int 长度
	 * @package app\api\model
	 */
	public static function strlength($str)
	{
		$arr = self::ch2arr($str);		#将字符串装换成数组
		$len = 0;
		foreach($arr as $v){
			if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$v)){
				$len = $len + 2;	#每个中文字符长度等于2
			}else{
				$len++;
			}
		}
		
		return $len;
	}
	
	#字符串转数组
	public static function ch2arr($str)
	{
		$length = mb_strlen($str, 'utf-8');
		$array = [];
		for ($i=0; $i<$length; $i++)
			$array[] = mb_substr($str, $i, 1, 'utf-8');
		return $array;
	}
	
	#随机字符串
	public static function randomString($length) {
		$str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$max = strlen($str) - 1;
		$result = '';
		for ($i = 0;$i < $length;$i++) {
			$result.= $str[rand(0, $max) ];
		}
		return $result;
	}
	
	/**
	 * 请求类型
	 * @param int $id 类型id
	 * @author paulxun
	 * @return array|static
	 */
	public static function type($id = 99)
	{
		$types = [
			'BCNotify1'		=> '系统对账',
			'Query Bill'	=> '信息查询',
			'Bill Charge'	=> '缴费销账',
		];
	    if ($id === 99) {
	        $result = $types;
	    }else{
			$result = '';
			if(isset($types[$id])) $result = $types[$id];
		}
	    return $result;
	}
}
